<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style> 
        div span{
            margin: 20px;
            background-color: #000000;
            color: #fff;
            width: 50px;
            height: 50px;
            text-align: center;
        }
    </style>
</head>
<body>
    <script>
        window.addEventListener('load',function(){

            //获取小时，分钟，秒的页面元素
            var hour = document.querySelector(".hour")
            var min = document.querySelector(".min")
            var sec = document.querySelector(".second")
            //输入要倒计时的时间点
            var inputTime = new Date('2022-03-11 00:00:00')
            //为了清除页面首次加载 以及刷新时候的页面空白效果 ，需要首次也执行倒计时函数
            countTime()
            //再摄入计时器
            setInterval(countTime,1000)

            //倒计时函数
            function countTime(){
            var nowTime = new Date() //返回的是当前时间总的毫秒数
            var times = (inputTime - nowTime) / 1000 //times是剩余时间总的毫秒数
            var h = parseInt(times / 60 / 60 % 24); //小时
            h = h < 10 ? '0' + h : h;
            hour.innerHTML = h;
            var m = parseInt(times / 60 % 60); //分
            var m = m < 10 ? '0' + m : m; 
            min.innerHTML = m;
            var s = parseInt(times % 60) //秒
            s = s<10?'0'+s : s
            sec.innerHTML = s;
        }
        })
        
    </script>
    <div>
        <span class="hour">1</span>
        <span class="min">2</span>
        <span class="second">3</span>
    </div>
</body>
</html>